package com.fengmoxi.ctv.manage;

import android.util.Log;

import androidx.annotation.NonNull;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;


public class LampManage {
    private static final String TAG = LampManage.class.getSimpleName();
    private static final String LAMP_SYSFS = "/sys/devices/platform/fe5a0000.i2c/i2c-1/1-0077/";

    public static class Position {
        private int pan;
        private int tilt;
        private int flip;

        public Position(int pan, int tilt, int flip) {
            this.pan = pan;
            this.tilt = tilt;
            this.flip = flip;
        }

        public int getPan() {
            return pan;
        }

        public void setPan(int val) {
            pan = val;
        }

        public int getTilt() {
            return tilt;
        }

        public void setTilt(int val) {
            tilt = val;
        }

        public int getFlip() {
            return flip;
        }

        public void setFlip(int val) {
            flip = val;
        }

        @NonNull
        public String toString() {
            return "Pan: " + pan + ", Tilt: " + tilt + ", Flip: " + flip;
        }
    }

    public static class UV {
        private int u;
        private int v;

        public UV(int u, int v) {
            this.u = u;
            this.v = v;
        }

        public int getU() {
            return u;
        }

        public void setU(int val) {
            u = val;
        }

        public int getV() {
            return v;
        }

        public void setV(int val) {
            v = val;
        }

        @NonNull
        public String toString() {
            return "U: " + u + ", V: " + v;
        }
    }

    private String read(String path) {
        try {
            FileInputStream fis = new FileInputStream(LAMP_SYSFS + path);
            byte[] buf = new byte[2048];
            if (fis.read(buf) != -1) {
                String str = new String(buf, StandardCharsets.UTF_8);
                return str.trim();
            }
        } catch (FileNotFoundException e) {
            Log.e(TAG, "found error: " + e.getMessage());
        } catch (IOException e) {
            Log.e(TAG, "IO error: " + e.getMessage());
        } catch (SecurityException e) {
            Log.e(TAG, "Security error: " + e.getMessage());
        } catch (Exception e) {
            Log.e(TAG, "error: " + e.getMessage());
        }
        return "";
    }

    private void write(String path, String value) {
        try {
            FileOutputStream fops = new FileOutputStream(LAMP_SYSFS + path);
            fops.write(value.getBytes());
            fops.flush();
            fops.close();
        } catch (FileNotFoundException e) {
            Log.e(TAG, "found error: " + e.getMessage());
        } catch (IOException e) {
            Log.e(TAG, "IO error: " + e.getMessage());
        }
    }

    private int parseInt(String str) {
        try {
            return Integer.parseInt(str);
        } catch (Exception e) {
            return 127;
        }
    }

    public Position getPosition() {
        String str = read("position");
        String[] values = str.split(",");
        return new Position(parseInt(values[0]), parseInt(values[1]), parseInt(values[2]));
    }

    public void setPosition(Position pos) {
        write("position", pos.getPan() + "," + pos.getTilt() + "," + pos.getFlip());
    }

    public int getBrightness() {
        String str = read("brightness");
        return parseInt(str);
    }

    public void setBrightness(int value) {
        write("brightness", String.valueOf(value));
    }

    public int getContrast() {
        String str = read("contrast");
        return parseInt(str);
    }

    public void setContrast(int value) {
        write("contrast", String.valueOf(value));
    }

    public UV getHue() {
        String str = read("hue");
        String[] values = str.split(",");
        return new UV(parseInt(values[0]), parseInt(values[1]));
    }

    public void setHue(UV hue) {
        write("hue", hue.getU() + "," + hue.getV());
    }

    public UV getSaturation() {
        String str = read("saturation");
        String[] values = str.split(",");
        return new UV(parseInt(values[0]), parseInt(values[1]));
    }

    public void setSaturation(UV saturation) {
        write("saturation", saturation.getU() + "," + saturation.getV());
    }

    public int getSharpness() {
        String str = read("sharpness");
        return parseInt(str);
    }

    public void setSharpness(int value) {
        write("sharpness", String.valueOf(value));
    }

    public String getSN() {
        return read("sn");
    }

    public String getVersion() {
        return read("version");
    }

    public String getTemperature() {
        return read("temperature");
    }

    public void initUserParam() {
        write("user_param_ctl", "0");
    }

    public void savePositionUserParam() {
        write("user_param_ctl", "2");
    }

    public void saveOptAdjUserParam(){
        write("user_param_ctl", "3");
    }

    public void saveBiphaseUserParam(){
        write("user_param_ctl", "4");
    }

    public void savePQUserParam(){
        write("user_param_ctl", "5");
    }

    private void setBiphaseCtl(String val){
        write("biphase_ctl", val);
    }

    public void BiphaseStart(){
        setBiphaseCtl("0");
    }

    public void BiphasePlus(){
        setBiphaseCtl("1");
    }

    public void BiphaseMinus(){
        setBiphaseCtl("2");
    }

    public void BiphaseExit(){
        setBiphaseCtl("3");
    }

    private void setOptAdjCtl(String val){
        write("opt_adj_ctl", val);
    }

    public void OptAdjStart(){
        setOptAdjCtl("0");
    }

    public void OptAdjPlus(){
        setOptAdjCtl("1");
    }

    public void OptAdjMinus(){
        setOptAdjCtl("2");
    }

    public void OptAdjExit(){
        setOptAdjCtl("3");
    }
}
